/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simchewie.algorithms;

import java.awt.Point;
import java.util.logging.Level;
import java.util.logging.Logger;
import simchewie.Labyrinth;
import navigation.*;
import config.*;

/**
 *
 * @author willi
 */
public class STPNavigator implements Wookie {

    int orientation = 0;
    Point pos = new Point(0, 0);
    private String[] directions = new String[]{"N", "O", "S", "W"};
    public Labyrinth labyrinth = null;
    boolean[][] washere = new boolean[10][12];
    Navigator henry = Config.getSearchRedPointNavigator();
    boolean isFinished = false;

    public STPNavigator(Labyrinth f, int x, int y) {
        labyrinth = f;
        pos.x = x;
        pos.y = y;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 12; j++) {
                washere[i][j] = false;

            }
        }

    }

    private void forward() {
        if (orientation == 0 && pos.y - 1 >= 0) {
            pos.y--;
        } else if (orientation == 1 && pos.x + 1 <= 9) {
            pos.x++;
        } else if (orientation == 2 && pos.y + 1 <= 11) {
            pos.y++;
        } else if (orientation == 3 && pos.x - 1 >= 0) {
            pos.x--;
        }

    }

    private void turnLeft() {
        orientation = (orientation + 3) % 4;
        henry.directionChanged(3);
    }

    private void turnRight() {
        orientation = (orientation + 1) % 4;
        henry.directionChanged(1);

    }

    private void turn180() {
        orientation = (orientation + 2) % 4;
        henry.directionChanged(2);
    }

    public void driveToNextField() {
        if (labyrinth.checkOrientation(pos.x, pos.y, orientation)) {
            forward();
        } else {
            if (labyrinth.checkOrientation(pos.x, pos.y, (orientation + 3) % 4)) {
                turnLeft();
                forward();
            } else if (labyrinth.checkOrientation(pos.x, pos.y, (orientation + 1) % 4)) {
                turnRight();
                forward();
            } else {
                turn180();
                forward();
            }
        }
    }

    public void nextstep() {


       
            try {


                if (labyrinth.countField(pos.x, pos.y) == 1) {
                    if (labyrinth.isFinish(pos.x, pos.y)) {
                        henry.moveOneStep();
                        henry.deadEndFound();
                        henry.redPointFound();
                        driveToNextField();
                        
                        henry = Config.getGoToExitNavigator(henry);
                       
                    } else {

                        henry.moveOneStep();
                        henry.deadEndFound();
                        driveToNextField();
                        
                    }

                } else if (labyrinth.countField(pos.x, pos.y) == 2) {
                    
                    System.out.println("blue");
                    henry.moveOneStep();

                    if (!henry.fieldIsKnown()) {
                        henry.addField(labyrinth.checkOrientation(pos.x, pos.y, orientation),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 1) % 4),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 2) % 4),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 3) % 4));
                    }
                    
                    int dir = henry.decideDirection();
                    System.out.println("blub 2" + dir);
                    if (dir == 0) {
                        forward();
                    } else if (dir == 1) {
                        turnRight();
                        forward();
                    } else if (dir == 2) {
                        turn180();
                        forward();
                    } else if (dir == 3) {
                        turnLeft();
                        forward();

                    } else if (dir == -1) {
                        isFinished = true;
                    }
                } else if (labyrinth.countField(pos.x, pos.y) == 3) {
                    
                    henry.moveOneStep();
                    if (!henry.fieldIsKnown()) {
                        henry.addField(labyrinth.checkOrientation(pos.x, pos.y, orientation),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 1) % 4),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 2) % 4),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 3) % 4));
                    }
                    int dir = henry.decideDirection();
                    if (dir == 0) {
                        forward();
                    } else if (dir == 1) {
                        turnRight();
                        forward();
                    } else if (dir == 2) {
                        turn180();
                        forward();
                    } else if (dir == 3) {
                        turnLeft();
                        forward();

                    } else if (dir == -1) {
                        isFinished = true;
                    }
                } else if (labyrinth.countField(pos.x, pos.y) == 4) {
                    
                    henry.moveOneStep();
                    if (!henry.fieldIsKnown()) {
                        henry.addField(labyrinth.checkOrientation(pos.x, pos.y, orientation),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 1) % 4),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 2) % 4),
                                labyrinth.checkOrientation(pos.x, pos.y, (orientation + 3) % 4));
                    }
                    int dir = henry.decideDirection();
                    if (dir == 0) {
                        forward();
                    } else if (dir == 1) {
                        turnRight();
                        forward();
                    } else if (dir == 2) {
                        turn180();
                        forward();
                    } else if (dir == 3) {
                        turnLeft();
                        forward();

                    } else if (dir == -1) {
                        isFinished = true;
                    }
                }
            } catch (Exception e) {

                System.out.println("Exception thrown!");
            }
            System.out.println(henry);
    }

    public Point getPosition() {
        return pos;


    }

    public int getOrientation() {
        return orientation;


    }

    public boolean isFinished() {
        return isFinished;


    }

    public boolean[][] getWasHere() {
        return washere;

    }
}
